<?php
namespace app\admin\controller;

use think\auth\Auth;
use think\Loader;
use think\Cache;
use think\Controller;
use think\Db;

/**
 * 后台公用基础控制器
 * Class AdminBase
 * @package app\common\controller
 */
class BaseController extends Controller {

	protected $site_config;

	protected function _initialize() {
		parent::_initialize();
		$this->checkAuth();
		$this->getMenu();
		// 输出当前请求控制器（配合后台侧边菜单选中状态）
		$this->assign('controller', Loader::parseName($this->request->controller()));
		$this->assign('action', Loader::parseName($this->request->action()));
		// 配置
		if (Cache::has('site_config')) {
			$site_config = Cache::get('site_config');
		} else {
			$site_config = Db::name('system')->field('value')->where('name', 'site_config')->find();
			$site_config = unserialize($site_config['value']);
			cache('site_config', $site_config);
		}
		$this->assign('site_config', $site_config);
		$this->site_config = $site_config;
		// 判断登录状态
		$user = Db::name('user')->field('session_id')->where('id', session('ADMIN.id'))->find();
		if ($user['session_id'] != session_id()) {
			//session('ADMIN', null);
			//$this->success('该账号已在别处登录！', 'login/index');
		}
	}
		//上传图片
	public function upload(){
		$file = request()->file('file');
		$info = $file -> move(ROOT_PATH . DS . 'uploads' );
		if ($info) {
			$result['code'] = 1;
			$result['msg'] = '上传成功！';
			$result['src'] = '/uploads/' . str_replace('\\', '/', $info -> getSaveName());
		} else {
			$result['code'] = 0;
			$result['msg'] = $file -> getError();
		}
		return json($result);
	}
	/**
	 * 权限检查
	 * @return bool
	 */
	protected function checkAuth() {
		if (!session('ADMIN')) {
			$this->redirect('admin/login/index');
		}
		$module = $this->request->module();
		$controller = $this->request->controller();
		$action = $this->request->action();
		// 排除权限
		$not_check = array(
			'admin/AuthGroup/getjson',
			'admin/Index/update'
		);
		if (!in_array($module . '/' . $controller . '/' . $action, $not_check)) {
			$auth = new Auth();
			$admin_id = session('ADMIN.id');
//			if (!$auth->check($module . '/' . $controller . '/' . $action, $admin_id) && $admin_id != 1) {
//				$this->error('没有权限');
//			}
		}
	}

	/**
	 * 获取侧边栏菜单
	 */
	protected function getMenu() {
		$menu = array();
		$admin_id = session('ADMIN.id');
		$auth = new Auth();
		$auth_rule_list = Db::name('auth_rule')->where('status', 1)->order(array(
			'sort' => 'asc'
		))->select();
		foreach ($auth_rule_list as $value) {
			if ($auth->check($value['name'], $admin_id) || $admin_id == 1) {
				$menu[] = $value;
			}
		}
		$menu = !empty($menu) ? array2tree($menu) : array();
		$this->assign('menu', $menu);
	}

	/**
	 * 通用状态设置
	 * 禁用、启用都是调用这个内部方法
	 * @return mixed
	 */
	public function status() {
		$table = input('param.table', strtolower(request()->controller()));
		$field = input('param.field', 'status');
		$ids = input('param.ids/a') ? input('param.ids/a') : input('param.id/a');
		$val = input('param.val', 1);
		if (empty($ids)) {
			return $this->error('参数传递错误[1]！');
		}
		if (empty($table)) {
			return $this->error('参数传递错误[2]！');
		}
		$res = db($table)->where('id', 'in', $ids)->setField($field, $val);
		if ($res === false) {
			$this->error('设置失败');
		}
		$this->success('设置成功');
	}

	/**
	 * 通用删除
	 * 单纯的记录删除
	 * @return mixed
	 */
	public function delete() {
		$ids = input('param.ids/a') ? input('param.ids/a') : input('param.id/a');
		$table = input('param.table', strtolower(request()->controller()));
		// 获取主键
		$res = db($table)->where('id', 'in', $ids)->delete();
		if ($res === false) {
			$this->error('删除失败');
		}
		$this->success('删除成功');
	}

	/**
	 * 通用排序
	 * @return mixed
	 */
	public function sorts() {
		$table = input('param.table', strtolower(request()->controller()));
		$field = input('param.field/s', 'sort');
		$ids = input('param.ids/d') ? input('param.ids/d') : input('param.id/d');
		$val = input('param.val/d', 0);
		$res = db($table)->where('id', 'in', $ids)->setField($field, $val);
		if ($res === false) {
			$this->error('排序失败');
		}
		$this->success('排序成功');
	}
	/**
	 * 通用审核
	 * 单纯的记录审核
	 * @return mixed
	 */
	public function audit() {
		$ids = input('param.id/d');
		$table = input('param.table', strtolower(request()->controller()));
		$val = input('param.audid/d', 0);
		$res = db($table)->where('id', $ids)->setField('auditing', $val);
		if ($res === false) {
			$this->error('修改失败');
		}
		$this->success('修改成功');
	}
}
